package cn.tedu.egnotice0905.controller;

import cn.tedu.egnotice0905.base.response.JsonResult;
import cn.tedu.egnotice0905.base.response.StatusCode;
import cn.tedu.egnotice0905.mapper.NoticeMapper;
import cn.tedu.egnotice0905.pojo.dto.NoticeAddParam;
import cn.tedu.egnotice0905.pojo.dto.NoticeListParam;
import cn.tedu.egnotice0905.pojo.dto.NoticeUpdateParam;
import cn.tedu.egnotice0905.pojo.entity.Notice;
import cn.tedu.egnotice0905.pojo.vo.NoticeDetailInfoVO;
import cn.tedu.egnotice0905.pojo.vo.NoticeListVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/notice")
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;

    @PostMapping("/add")
    public JsonResult addNotice(@RequestBody NoticeAddParam noticeAddParam) {
        log.info("添加数据 :", noticeAddParam);

        Notice notice = new Notice();

        BeanUtils.copyProperties(noticeAddParam, notice);

        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        notice.setUserId(2L);

        int i = noticeMapper.insert(notice);

        log.debug("notice信息:" + notice);

        if (i > 0) {
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }

    @GetMapping("/list")
    public JsonResult getNoticeList(NoticeListParam noticeListParam) {
        log.debug("查询参数:{}", noticeListParam);

        List<NoticeListVo> list = noticeMapper.selectList(noticeListParam);

        return JsonResult.ok(list);
    }

    @GetMapping("/detail")
    public JsonResult getNoticeDetail(Long noticeId) {
        log.debug("查询编号为:" + noticeId);

        NoticeDetailInfoVO noticeDetailInfoVO = noticeMapper.selectDtl(noticeId);

        return JsonResult.ok(noticeDetailInfoVO);
    }

    @PostMapping("/del")
    public JsonResult delNotice(Long noticeId) {
        log.debug("删除编号为:" + noticeId);

        int i = noticeMapper.delById(noticeId);

        if (i > 0) {
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.OPERATION_FAILED);

    }

    @PostMapping("/update")
    public JsonResult updateNotice(@RequestBody NoticeUpdateParam noticeUpdateParam) {
        log.debug("修改参数:" + noticeUpdateParam);

        int i = noticeMapper.update(noticeUpdateParam);

        if (i > 0) {
            return JsonResult.ok();
        }

        return new JsonResult(StatusCode.OPERATION_FAILED);
    }

}
